import _regeneratorRuntime from "@babel/runtime/regenerator";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import request from 'utils/request';
import { CPAAS_MSG } from 'utils/config';
/**
 * 查询配置列表数据
 * @async
 * @function fetchSMSList
 * @param {object} params - 查询条件
 * @param {!number} [params.page = 0] - 数据页码
 * @param {!number} [params.size = 10] - 分页大小
 * @returns {object} fetch Promise
 */

export function fetchReceiveConfig() {
  return _fetchReceiveConfig.apply(this, arguments);
}
/**
 * 保存
 * @async
 * @function saveConfig
 * @param {object} params - 请求参数
 * @returns {object} fetch Promise
 */

function _fetchReceiveConfig() {
  _fetchReceiveConfig = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            return _context.abrupt("return", request("".concat(CPAAS_MSG, "/v1/user-receive-configs"), {
              method: 'GET'
            }));

          case 1:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _fetchReceiveConfig.apply(this, arguments);
}

export function saveConfig(_x) {
  return _saveConfig.apply(this, arguments);
}
/**
 * 处理接口获取的数据，提取每个节点的层次路径
 * @param {array} collections - 页面展示数据
 * @param {array} levelPath - 特定组织的层级路径
 * @returns {object} 节点树和层次路径组成的对象
 */

function _saveConfig() {
  _saveConfig = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(params) {
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            return _context2.abrupt("return", request("".concat(CPAAS_MSG, "/v1/user-receive-configs"), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _saveConfig.apply(this, arguments);
}

export function renderTreeData() {
  var collections = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var levelPath = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var pathMap = levelPath;
  var renderTree = collections.map(function (item) {
    var temp = item;
    pathMap[temp.receiveId] = [].concat(_toConsumableArray(pathMap[temp.parentReceiveId] || []), [temp.receiveId]);

    if (temp.children) {
      temp.children = _toConsumableArray(renderTreeData(temp.children || [], pathMap).renderTree);
    }

    return temp;
  });
  return {
    renderTree: renderTree,
    pathMap: pathMap
  };
}